import { reqGetCode, reqRegister, reqUserLogin } from "@/api"

export default {
    state: {
        code: '',
        token: ''
    },
    mutations: {
        GETCODE(state, code) {
            state.code = code
        },
        USERLOGIN(state, token) {
            state.token = token
        }
    },
    actions: {
        //获取用户验证码
        async getCode({ commit }, phone) {
            let res = await reqGetCode(phone)
            console.log(commit)
            console.log(res)
            if (res.code == 200) {
                commit('GETCODE', res.data);
                return "ok"
            } else {
                return Promise.reject(new Error('faile'))
            }
        },
        //用户注册
        async userRegister({ commit }, user) {
            let res = await reqRegister(user)
            console.log(res)
            console.log(commit)
            if (res.code == 200) {
                return "ok"
            } else {
                return Promise.reject(new Error('faile'))
            }
        },
        //用户登录
        async userLogin({ commit }, data) {
            let res = await reqUserLogin(data);
            console.log(commit)
            console.log(res)
            if (res.code == 200) {
                commit('USERLOGIN', res.data.token)
                return "ok"
            } else {
                return Promise.reject(new Error('faile'))
            }
        }
    },
    getters: {}
}